package com.ruoyi.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.UserDrugInfoWrapper;
import com.ruoyi.common.core.service.UserEventCommonService;
import com.ruoyi.common.enums.UserBusinessType;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.user.domain.UserHelpGroup;
import com.ruoyi.user.domain.UserInfo;
import com.ruoyi.user.domain.vo.relation.RelationWrapper;
import com.ruoyi.user.mapper.UserHelpGroupMapper;
import com.ruoyi.user.mapper.UserInfoMapper;
import com.ruoyi.user.service.RelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 吸毒人员通用接口
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2023/2/4 21:24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/common")
public class UserDrugCommonController {

    private final UserInfoMapper userInfoMapper;
    private final SysUserMapper sysUserMapper;
    private final UserHelpGroupMapper userHelpGroupMapper;
    private final RelationService relationService;

    @GetMapping("loadHelpGroupByDeptId/{deptId}")
    public R<List<UserHelpGroup>> loadHelpGroupByDeptId(@PathVariable("deptId") Long deptId) {
        LambdaQueryWrapper<SysUser> queryWrapper = Wrappers.<SysUser>lambdaQuery()
            .eq(SysUser::getDeptId, deptId)
            .eq(SysUser::getBusinessType, UserBusinessType.USER_MEMBER)
            .isNotNull(SysUser::getBusinessType);
        List<SysUser> userList = sysUserMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(userList)) {
            return R.ok();
        }
        List<Long> userIdList = userList.stream().map(SysUser::getBusinessId).collect(Collectors.toList());
        List<UserInfo> userInfoList = userInfoMapper.selectList(Wrappers.<UserInfo>lambdaQuery().in(UserInfo::getFzr, userIdList));
        List<Long> businessUserIdList = userInfoList.stream().map(UserInfo::getId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(businessUserIdList)) {
            return R.ok();
        }
        List<UserHelpGroup> userHelpGroupList = userHelpGroupMapper.selectList(Wrappers.<UserHelpGroup>lambdaQuery().in(UserHelpGroup::getUserId, businessUserIdList));
        return R.ok(userHelpGroupList);
    }

    private final UserEventCommonService userEventCommonService;


    /**
     * 移动端人员查询附属信息：谈话日期，尿检日期，签到日期等
     *
     * @param userId
     * @return
     */
    @GetMapping("wrapperInfo/{userId}")
    public R<UserDrugInfoWrapper> getWrapperInfo(@PathVariable("userId") Long userId) {
        UserDrugInfoWrapper wrapper = new UserDrugInfoWrapper();
        wrapper.setUserId(userId);
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo != null) {
            wrapper.setAddress(userInfo.getJzdxz());
            wrapper.setStatus(userInfo.getTermType());
        }
        userEventCommonService.renderUserDrugInfoWrapper(wrapper);
        return R.ok(wrapper);
    }

    /**
     * 获取吸毒人员关系图数据
     *
     * @param userId
     * @return
     */
    @GetMapping("getUserRelation/{userId}")
    public R<RelationWrapper> getUserRelation(@PathVariable("userId") Long userId) {
        return R.ok(relationService.buildRelation(userId));
    }

}
